package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }




    /**
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping ("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查找：{}", employeePageQueryDTO);
      PageResult pageResult=  employeeService.pageselect(employeePageQueryDTO);


        return Result.success(pageResult);
    }

    /**
     *根据id查询数据
     * @param id
     * @return
     */
   @GetMapping("/{id}")
   @ApiOperation("id查询员工")
    public Result<Employee > selectIdsEmploye(@PathVariable Long id) {
        log.info("员工id查找：{}", id);
        if(id==null){
            return  Result.error("id是空的");
        }
       Employee employee= employeeService.selectIdsEmploye(id);

        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation("员工跟新查询")
    public Result<String > update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("员工跟新：{}", employeeDTO);
        employeeService.update(employeeDTO);



        return Result.success();
    }

    /**
     * 启用员工
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工")
    public Result<String > enableDisable(@PathVariable int status,long id) {
        log.info("员工启用和增加：{},{}", status,id);
        employeeService.enDi(status,id);



        return Result.success();
    }

    /**
     *员工增加
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加员工")
    public Result<String > add(@RequestBody EmployeeDTO employeeDTO) {
        log.info("员工跟新：{}", employeeDTO);
        employeeService.add(employeeDTO);

        return Result.success();
    }
    /**
     * 退出
     *
     * @return
     */

    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

}
